package com.atguigui.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 901.股票价格跨度
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/10/21 8:59
 */
public class P901OnlineStockSpan {

    public static void main(String[] args) {
        StockSpanner solution = new P901OnlineStockSpan().new StockSpanner();
        // TO TEST
    }

    class StockSpanner {
        //队列
        Deque<int[]> stack;
        int idx;

        public StockSpanner() {
            stack = new ArrayDeque<int[]>();
            stack.push(new int[]{-1, Integer.MAX_VALUE});
            idx = -1;
        }

        public int next(int price) {
            //索引不断增加
            idx++;
            while (price >= stack.peek()[1]) {
                //吐出
                stack.pop();
            }
            //吐出一个后计算和前面的差值
            int ret = idx - stack.peek()[0];
            //如果一直是递减，增相差永远是1
            stack.push(new int[]{idx, price});
            return ret;
        }
    }
}
